﻿using System.Collections.Generic;
using Data;
using UnityEngine;
using UnityEngine.UI;

namespace Demo
{
    public class RedDotDataNumBagDemo : MonoBehaviour
    {
        public GameObject bagWindow;
        public Button bagButton;
        public Button closeButton;
        public GameObject bagItem;
        public Transform[] itemCellArr;

        bool isCreate = false;
        void Awake()
        {
            for (int i = 0; i < 15; i++)
            {
                RedDotDataMgr.BagDataList.Add(new BagData
                {
                    id = i,
                    count = i + 1
                });
            }
            bagButton.onClick.AddListener(OnBagButtonClick);
            closeButton.onClick.AddListener(OnCloseButtonClick);
            //注册大厅背包数量红点
            RedDotTreeNode bagNode = new RedDotTreeNode
            {
                redDotType = RedDotType.RedDotDataNum,
                node = RedDotDefine.BagRoot,
                logicHander = OnRedDotBagLogicHandler
            };
            RedDotSystem.Instance.InitlizateRedDotTree(new List<RedDotTreeNode>
            {
                bagNode
            });
        }
        public void OnRedDotBagLogicHandler(RedDotTreeNode redNode)
        {
            redNode.redDotCount = 0;
            foreach (var item in RedDotDataMgr.BagDataList)
            {
                if (item.isRead == false)
                {
                    redNode.redDotCount++;
                }
            }
            Debug.Log("redNode.redDotCount:" + redNode.redDotCount);
        }

        public void OnBagButtonClick()
        {
            bagWindow.SetActive(true);
            if (isCreate == false)
            {
                for (int i = 0; i < RedDotDataMgr.BagDataList.Count; i++)
                {
                    GameObject obj = Instantiate(bagItem, itemCellArr[i]);
                    obj.SetActive(true);
                    obj.transform.localPosition = Vector3.zero;
                    obj.transform.localScale = Vector3.one;
                    obj.transform.rotation = Quaternion.identity;
                    BagItem item = obj.GetComponent<BagItem>();
                    item.SetData(RedDotDataMgr.BagDataList[i]);
                }
                isCreate = true;
            }

        }
        public void OnCloseButtonClick()
        {
            bagWindow.SetActive(false);
        }
    }
}